//
//  SearchView.swift
//  SwiftDemo
//
//  Created by 李文强 on 2017/1/13.
//  Copyright © 2017年 李文强. All rights reserved.
//

import UIKit

protocol SearchBarClickDelegate {
    func searchBarHasClick()
    func searchDidEndClick()
}

class SearchView: UIView ,UISearchControllerDelegate,UISearchResultsUpdating,UISearchBarDelegate{

    var delegate:SearchBarClickDelegate?
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    let searchBar = UISearchBar()
    
    func imageWithColor(color:UIColor) ->UIImage
    {
        let rect = CGRect(x:0,y:0,width:Screen.width,height:40)
        UIGraphicsBeginImageContext(rect.size)
        let context:CGContext = UIGraphicsGetCurrentContext()!
        context.setFillColor(color.cgColor)
        context.fill(rect)
        let image:(UIImage) = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
        
    }

    //MARK:Create Custom SearchBar
    func createSearchBar() -> Void {
        
        self.searchBar.isTranslucent = true
        self.searchBar.delegate = self
        self.searchBar.placeholder = "Search"
        self.searchBar.backgroundColor = UIColor.white
        self.searchBar.backgroundImage = imageWithColor(color: UIColor.init(colorLiteralRed: 0.4941, green: 0.7412, blue: 0.9490, alpha: 1))
        self.searchBar.keyboardType = UIKeyboardType.default
        //MARK:输入框的背景
//        let seachTextFeild = self.searchBar.subviews.first?.subviews.last
//        seachTextFeild?.backgroundColor = UIColor.blue
//        self.searchBar.setShowsCancelButton(true, animated: true)
        self.addSubview(self.searchBar)
        self.searchBar.mas_makeConstraints { (make:MASConstraintMaker?) in
            make!.left.equalTo()(0)
            make!.right.equalTo()(0)
            make!.top.equalTo()(0)
            make!.bottom.equalTo()(0)
        }
        
        //取消searchbar背景色
//        - (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
//        {
//            CGRect rect = CGRectMake(0, 0, size.width, size.height);
//            UIGraphicsBeginImageContext(rect.size);
//            CGContextRef context = UIGraphicsGetCurrentContext();
//            
//            CGContextSetFillColorWithColor(context, [color CGColor]);
//            CGContextFillRect(context, rect);
//            
//            UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//            UIGraphicsEndImageContext();
//            
//            return image;
//        }
        
//        searchBar.searchResultsUpdater = self
//        //默认情况下，UISearchController暗化前一个view，这在我们使用另一个view controller来显示结果时非常有用，但当前情况我们并不想暗化当前view，即设置开始搜索时背景是否显示
//        searchBar.dimsBackgroundDuringPresentation = false
//        searchBar.searchBar.placeholder = "Search more..."
//        searchBar.searchBar.delegate = self
//        //设置searchBar自适应大小
//        searchBar.searchBar.sizeToFit()
//        //设置definesPresentationContext为true，我们保证在UISearchController在激活状态下用户push到下一个view controller之后search bar不会仍留在界面上。
//        searchBar.definesPresentationContext = true
//        self.addSubview(searchBar.searchBar)
////        searchBar.searchBar.mas_makeConstraints { (make:MASConstraintMaker?) in
////                make!.left.equalTo()(0)
////                make!.right.equalTo()(0)
////                make!.top.equalTo()(0)
////                make!.bottom.equalTo()(0)
////        }

        //MARK:regist touch
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(registeTap))
        self.addGestureRecognizer(tap)
        
    }
    func registeTap() -> Void {
        self.searchBar.resignFirstResponder()
    }
    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        print(searchBar.text!)
    }
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        print(searchBar.text!)
         self.searchBar.resignFirstResponder()
    }
    func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
        self.beginClickFunc()
        return true
    }
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
           self.delegate?.searchDidEndClick()
    }
    func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
     
        return true
    }
    func updateSearchResults(for searchController: UISearchController) {
        
    }
    //MARK:开始点击后调用的方法
    func beginClickFunc() -> Void {

        self.delegate?.searchBarHasClick()
    }
}

